﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using NetAcademia.Configuration;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
using System.Web.Routing;
using System.Web;
using System.Web.Mvc;

namespace NetAcademia.Web.Mvc.ImageRendering
{
    public static class TacepaoHelper
    {

        #region img
        public static HtmlString Tacepao(this HtmlHelper html, string key, string text)
        {
            return Tacepao(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: null, htmlAttributes: null);
        }

        public static HtmlString Tacepao(this HtmlHelper html, string key, string text, object sqlParameters, object htmlAttributes)
        {
            return Tacepao(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes);
        }

        public static HtmlString Tacepao(this HtmlHelper html, string key, string text, string icon)
        {
            return Tacepao(html, controller: null, action: null, key: key, text: text,  rotation: null, contentRotation: null, icon: icon, sqlParameters: null, htmlAttributes: null);
        }

        public static HtmlString Tacepao(this HtmlHelper html, string key, string text, string icon, object sqlParameters, object htmlAttributes)
        {
            return Tacepao(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes);
        }

        public static HtmlString Tacepao(this HtmlHelper html, string key, string text, string icon, double? rotation, double? contentRotation, object sqlParameters, object htmlAttributes)
        {
            return Tacepao(html, controller: null, action: null, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon,  sqlParameters: sqlParameters, htmlAttributes: htmlAttributes);
        }

        public static HtmlString Tacepao(this HtmlHelper html, string controller, string action, string key, string text, double? rotation, double? contentRotation, string icon, object sqlParameters, object htmlAttributes)
        {
            return TacepaoHtml(html, controller: controller, action: action, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon,  sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, tag: RenderingTags.img);
        }
        #endregion

        #region linked
        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, string linkUrl)
        {
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: null, htmlAttributes: null, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, string icon, string linkUrl)
        {
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon, sqlParameters: null, htmlAttributes: null, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, string icon, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, string icon, double? rotation, double? contentRotation, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string controller, string action, string key, string text, double? rotation, double? contentRotation, string icon, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return TacepaoHtml(html, controller: controller, action: action, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, tag: RenderingTags.img, linkUrl: linkUrl);
        }    
        /**********************************************************************************************************************************************************************/
        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: null, htmlAttributes: null, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, string icon, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon, sqlParameters: null, htmlAttributes: null, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, string icon, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string key, string text, string icon, double? rotation, double? contentRotation, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return TacepaoLinked(html, controller: null, action: null, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, linkUrl: linkUrl);
        }

        public static HtmlString TacepaoLinked(this HtmlHelper html, string controller, string action, string key, string text, double? rotation, double? contentRotation, string icon, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return TacepaoHtml(html, controller: controller, action: action, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, tag: RenderingTags.img, linkUrl: linkUrl);
        }
        #endregion

        #region TacepaoInput
        public static HtmlString TacepaoInput(this HtmlHelper html, string key, string text)
        {
            return TacepaoInput(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: null, htmlAttributes: null);
        }

        public static HtmlString TacepaoInput(this HtmlHelper html, string key, string text, object sqlParameters, object htmlAttributes)
        {
            return TacepaoInput(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: null, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes);
        }

        public static HtmlString TacepaoInput(this HtmlHelper html, string key, string text, string icon)
        {
            return TacepaoInput(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon, sqlParameters: null, htmlAttributes: null);
        }

        public static HtmlString TacepaoInput(this HtmlHelper html, string key, string text, string icon, object sqlParameters, object htmlAttributes)
        {
            return TacepaoInput(html, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes);
        }

        public static HtmlString TacepaoInput(this HtmlHelper html, string key, string text, string icon, double? rotation, double? contentRotation, object sqlParameters, object htmlAttributes)
        {
            return TacepaoInput(html, controller: null, action: null, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes);
        }

        public static HtmlString TacepaoInput(this HtmlHelper html, string controller, string action, string key, string text, double? rotation, double? contentRotation, string icon, object sqlParameters, object htmlAttributes)
        {
            return TacepaoHtml(html, controller: controller, action: action, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon, sqlParameters: sqlParameters, htmlAttributes: htmlAttributes, tag: RenderingTags.input);
        }
        #endregion

        #region TacepaoUrl

        public static HtmlString TacepaoUrl(this UrlHelper url, string key, string text)
        {
            return TacepaoUrl(url, controller: null, action: null, key: key, text: text,  rotation: null, contentRotation: null, icon: null, sqlParameters: null);
        }

        public static HtmlString TacepaoUrl(this UrlHelper url, string key, string text, object sqlParameters)
        {
            return TacepaoUrl(url, controller: null, action: null, key: key, text: text,  rotation: null, contentRotation: null, icon: null,  sqlParameters: sqlParameters);
        }

        public static HtmlString TacepaoUrl(this UrlHelper url, string key, string text, string icon)
        {
            return TacepaoUrl(url, controller: null, action: null, key: key, text: text,  rotation: null, contentRotation: null, icon: icon,  sqlParameters: null);
        }

        public static HtmlString TacepaoUrl(this UrlHelper url, string key, string text, string icon, object sqlParameters)
        {
            return TacepaoUrl(url, controller: null, action: null, key: key, text: text, rotation: null, contentRotation: null, icon: icon,  sqlParameters: sqlParameters);
        }

        public static HtmlString TacepaoUrl(this UrlHelper url, string key, string text, string icon, double? rotation, double? contentRotation, object sqlParameters)
        {
            return TacepaoUrl(url, controller: null, action: null, key: key, text: text, rotation: rotation, contentRotation: contentRotation, icon: icon, sqlParameters: sqlParameters);
        }

        public static HtmlString TacepaoUrl(this UrlHelper url, string controller, string action, string key, string text, double? rotation, double? contentRotation, string icon,  object sqlParameters)
        {
            string tacepaoUrl = GetTacepaoUrl(url, controller, action, key, text,  rotation, contentRotation, icon, sqlParameters);
            return new HtmlString(tacepaoUrl);
        }

        #endregion


        private static HtmlString TacepaoHtml(this HtmlHelper html, string controller, string action, string key, string text, double? rotation, double? contentRotation, string icon, object sqlParameters, object htmlAttributes, RenderingTags tag, string linkUrl = null)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string imageUrl = GetTacepaoUrl(url, controller, action, key, text, rotation, contentRotation, icon, sqlParameters);

            return XamlToPngHelper.GenerateHtml(imageUrl, htmlAttributes, tag, anchorUrl: linkUrl);
        }

        private static string GetTacepaoUrl(UrlHelper url, string controller, string action, string key, string text, double? rotation, double? contentRotation, string icon,  object sqlParameters)
        {
            if (string.IsNullOrWhiteSpace(action))
            {
                controller = "XAMLtoMVC";
                action = "Tacepao";
            }

            string imageUrl = url.RouteUrl(new { controller = controller, action = action, id = key, text = text,  rotation = rotation, contentRotation = contentRotation, icon = icon });


            if (sqlParameters != null)
            {
                var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(sqlParameters);
                StringBuilder sb = new StringBuilder(attributes.Count * 30);
                //fel kell építeni a query stringet
                foreach (var item in attributes)
                {
                    sb.AppendFormat("&_{0}={1}", HttpUtility.UrlEncode(item.Key), HttpUtility.UrlEncode(string.Format("{0}", item.Value)));
                }

                if (!imageUrl.Contains('?')) //már van valami a querystring-ben
                {
                    sb.Remove(0, 1);
                    sb.Insert(0, '?');
                }
                imageUrl += sb.ToString();
            }

            return imageUrl;
        }

    }
}
